import { formatImage } from '../../utils/formatImage';
import { formatPrice } from '../../utils/util';
import { dateFormat } from '../../utils/dateFormat';

Component({
    properties: {
        lists: {
            type: Array,
            value: []
        },
        uploadParam: {
            type: Object,
            value: {}
        }
    },
    data: {
        goodsLists: [],
        sysWidth: wx.getSystemInfoSync().windowWidth,
        sysHeight: wx.getSystemInfoSync().windowHeight,
        entrenceTime: dateFormat('yyyy-MM-dd hh:mm:ss:S'),
    },
    lifetimes: {
        attached: function() {
            // wx.showLoading({
            //     title: '加载中',
            // });
        },
    },
    methods: {
        jumpGoods(e) {
            const { gid, index } = e.currentTarget.dataset;
            const { uploadParam, entrenceTime } = this.data;
            let opt_time = dateFormat('yyyy-MM-dd hh:mm:ss:S');

            wx.navigateTo({
                url: '/pages/goods/goodsDetail/goodsDetail?gid='+ gid +'&channel=' + uploadParam.channel + '&ref_page_id=' + uploadParam.ref_page_id + '&ref_opt_resource_type=' + uploadParam.ref_opt_resource_type + '&ref_opt_protocol='+ uploadParam.ref_opt_protocol +'&opt_resource_id=' + gid + '&opt_resource_type=5&opt_protocol=' + uploadParam.opt_protocol + '&opt_time=' + opt_time + '&log_type=1&ref_write_sever_timestamp=' + entrenceTime + '&opt_resource_area_index=' + index
            })
        },
    },
    observers: {
        "lists": function(n) {
            if (n && n.length) {
                const goods_channel_source_c = ['国内现货', '海外现货', '代购', '现货', '预售' ];

                n.forEach((item, index) => {
                    item.product_cover_image.full_path = formatImage.cdnImage(item.product_cover_image.path, formatImage.THUMBNAIL_M);
                    item.goods_channel_source = item.goods_channel_source == 4 ? 1 : item.goods_channel_source;
                    // item.new_goods_price = item.goods_price ? formatPrice(item.goods_price / 100) : ''
                    // 优先展示含税价
                    const coupon_price = item.use_coupon_info ? item.use_coupon_info.coupon_price : 0 ;
                    if (item.tax_price) { 
                        item.new_goods_price = formatPrice((item.tax_price - coupon_price) / 100);
                        item.new_original_price = item.original_price ? formatPrice(item.original_price / 100) : '';
                    } else {
                        if (item.tax_info && item.tax_info.tax) {
														item.new_goods_price = formatPrice(Math.ceil((item.goods_price - coupon_price + item.tax_info.tax)/ 100)); 
                        } else {
                            item.new_goods_price = formatPrice((item.goods_price - coupon_price)/ 100);
                        }
                        
                        if (item.use_coupon_info) {
                            item.new_original_price = item.original_price ? formatPrice(item.original_price / 100) : formatPrice(item.goods_price / 100);
                        } else {
                            item.new_original_price = item.original_price ? formatPrice(item.original_price / 100) : '';
                        }
                    }

                    item.goods_channel_source_c = item.goods_channel_source == 1 && item.is_mf_shop == 1 ? '极速发' : goods_channel_source_c[item.goods_channel_source - 1];

                });
            }

            // wx.hideLoading();
            this.setData({
                goodsLists: n
            })
        }
    },
})
